from Products.KnowledgeEngine.KERL.Engine import Engine
from Products.KnowledgeEngine.Assessment import Assessment

print "<html>"

# Test1 - the big test of everything
test1 = ['all',
           ['all', 
                   ["assessment", "createdbefore", "2009-01-01"], 
                   ["assessment", "createdafter", "2006-01-01"]],
                   
           ['any', ['currentuser','ingroup','Foo'],
                   ['currentuser','notingroup','Foo']],
                   
           ['all', 
                   ["component", "equals", "bar"], 
                   ["component","notequals","Hello"],
                   ["opt01181706181959719785117","selected"],
                   ["mch0118188554300470111930617","selected","A"]],
                                      
           ['none', ["component",'contains','a'],
                    ["component","greaterthan",8.0],
                    ["component","greaterthanequal",7.0],
                    ["component","lessthan",3.0],
                    ["component","lessthanequal",2.0]],                    
                   
                  
        ]

test2 = ["component","equals","Hello"]

test3 = ['all', ['assessment', 'createdbefore', '2009-01-01'], ['assessment', 'createdafter', '2007-01-01']]

test4 = ["text01181706261878808631801", "equals", "Foo"]

test5 = ['any',
            ['text01173323039430345970121', 'equals', 'Hello'],
            ['opt01181706181959719785117','selected']
        ]

test6 = ['reference','previous','TextBox']

test7 = [['reference','previous','TextBox'], 'equals','Hello']

test8 = ['currentuser','ingroup','Manager']

test9 = ['text01173323039430345970121','contains','ell']

test2_10 = ["opt011678877910618701860094","selected"]

test11 = ['chk01182138592314657505643','selected']

test12 = ['opt011904936710100974856923','selected']

test13 = ["text01173323039430345970121",'inrange',5,10]

test14 = ["assessment", "inrepository", "admin"]

ass = context.knowledgeengine_assessmentstorageservice

#assessment1 = ass.loadAssessment("Brands-28")
assessment1 = ass.loadAssessment("aid01190592497345347381836")
#assessment2 = ass.loadAssessment("aid01189990434853628093537")
#component  = context.getComponentByIdentity('text01173323039430345970121')
#component  = context.getComponentByIdentity('text01181706261878808631801')
component  = context.getComponentByIdentity('opt011904936710100974856923')
#component2 = context.getComponentByIdentity('para01167888009591000592978')
section    = component.aq_parent.aq_parent
model      = assessment1.getModel()


namespace = {'component':  component,
             'model':      model,
}

#print Engine().compile( test1, namespace ).toEnglish()
#print Engine().compile( test1, namespace ).execute( assessment1 )

#print "SELECT * FROM assessments WHERE "

#compiled = Engine().compile( test12, model, component, lazyDependancies=True )
compiled = Engine().compile( test14, model, component )
optimized = compiled.optimize( assessment1, section )

#print optimized.toJavascript()




print compiled.toKERL()
print "<hr>"
#print compiled.toJavascript()
#print "<hr>"
print compiled.toEnglish()
print "<hr>"
print compiled.toSQL()
print "<hr>"
print "<hr>"
print "<hr>"
print optimized.toKERL()
print "<hr>"
print optimized.toJavascript()
print "<hr>"
print optimized.toEnglish()
print "<hr>"
print optimized.toSQL()
print "<hr>"

print optimized.toSQL() 

return printed 